import time
import os, sys, gc
gc.disable()

def main(a, a2=5000):

    a2= int(a2)

    s= open(a, encoding='UTF8', mode='r')
    if os.getcwd()==os.path.dirname(os.path.abspath(__file__)):
        os.chdir(os.path.dirname(a))
    s7 = open("%s.%s.txt" %(os.path.basename(a)[:-4], time.time()), encoding='UTF8', mode='w')
    s2= s.readlines()

    s3= []
    while s2:
        s4= next((s2[:n] for n in range(len(s2), 0, -1) if len(' '.join(s2[:n]))<a2), s2[:1])
        s3.append(''.join(s4))
        s2= s2[len(s4):]

    s6= []
    for i in range(len(s3)):
        s8=s3[i]
        if len(s8)>a2:
            s6.append('%s:\t%s\t...\t%s' %(len(s8), s8[:20], s8[-20:]))
    if len(s6)>0:
        print("存在超过%s字的分段" %a2)
        print('\n'.join(s6))
    
    
    for i in s3:
        s7.write(i)
        s7.write('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n')
    
    s.close()
    s7.close()

if __name__ == "__main__":
    if not len(sys.argv) <2:
        for i in sys.argv[1:]:
            if i[-4:] == '.txt':
                main(i.strip('\'\"\\').rstrip("/"))
    else:
        print("\tTo Split a txt with a certain number of words:")
        print("\tpython *this [*txt.path]")
        
